package cczxsong;

import edu.princeton.cs.algs4.StdOut;

public class test {
    public static void main(String[] args) {
        try {
            int count = 5;

            long begin = System.currentTimeMillis();
            String s1 = testString(count);
            long end = System.currentTimeMillis();
            long time = end - begin;
            System.out.println("String 方法拼接"+count+"次消耗时间：" + time + "毫秒");

            begin = System.currentTimeMillis();
            String s2 = testStringBuilder(count);
            end = System.currentTimeMillis();
            time = end - begin;
            System.out.println("StringBuilder 方法拼接"+count+"次消耗时间：" + time + "毫秒");
            StdOut.println(s1);
            StdOut.println(s2);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static String testString(int count) {
        String result = "";

        for (int i = 0; i < count; i++) {
            result += "hello ";
        }

        return result;
    }

    private static String testStringBuilder(int count) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < count; i++) {
            sb.append("hello ");
        }

        return sb.toString();
    }
}
